import 'package:flutter/material.dart';
import 'package:universally/universally.dart';

const titleHeight = 30.0;

class SliverAppBarPage extends StatelessWidget {
  const SliverAppBarPage({super.key});

  @override
  Widget build(BuildContext context) {
    return BaseScaffold(
      body: NestedScrollView(
        headerSliverBuilder:
            (_, __) => [
              SliverAppBar(
                pinned: true,
                expandedHeight: FlexibleSpaceBarLayoutBuilder.getExpandedHeight(
                  context,
                  titleHeight,
                ),
                flexibleSpace: FlexibleSpaceBarLayoutBuilder(
                  builder:
                      (_, padding) => FlexibleSpaceBar(
                        expandedTitleScale: 1,
                        title: SizedBox.expand(child: buildTitle),
                        titlePadding: padding,
                      ),
                  contentHeight: titleHeight,
                ),
              ),
            ],
        body: BaseList(
          itemCount: Colors.accents.length,
          separatorBuilder: (_, __) => Divider(),
          itemBuilder: (_, int index) {
            final color = Colors.accents[index];
            return Container(color: color, width: double.infinity, height: 100);
          },
        ),
      ),
    );
  }

  Widget get buildTitle => Universal(
    safeTop: true,
    direction: Axis.horizontal,
    padding: EdgeInsets.only(left: 15),
    children: [
      ClipOval(child: Container(width: 40, height: 40, color: Colors.red)),
      12.widthBox,
      Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [TextLarge('SliverAppBar'), TextSmall('SliverAppBa subBar')],
      ),
    ],
  );
}

typedef FlexibleSpaceBarLayoutBuilderTitleBuilder =
    Widget Function(double expanded, EdgeInsetsGeometry padding);

class FlexibleSpaceBarLayoutBuilder extends StatelessWidget {
  const FlexibleSpaceBarLayoutBuilder({
    super.key,
    required this.builder,
    required this.contentHeight,
  });

  final FlexibleSpaceBarLayoutBuilderTitleBuilder builder;
  final double contentHeight;

  static double getExpandedHeight(BuildContext context, double titleHeight) =>
      titleHeight + context.statusBarHeight.round() + kToolbarHeight;

  @override
  Widget build(BuildContext context) {
    final statusBarHeight = context.statusBarHeight.round();
    return LayoutBuilder(
      builder: (context, constraints) {
        double maxHeight =
            constraints.maxHeight.roundToDouble() -
            statusBarHeight * 2 -
            kToolbarHeight;
        double expanded = maxHeight / contentHeight;
        if (expanded < 0) expanded = 0;
        if (expanded > 1) expanded = 1;
        return builder(
          expanded,
          EdgeInsets.only(
            left: kToolbarHeight - kToolbarHeight * expanded,
            top: kToolbarHeight * expanded,
          ),
        );
      },
    );
  }
}
